// 主页页面设计

// 关闭VSCode非必要设计
// ignore_for_file: camel_case_types, prefer_const_constructors_in_immutables, non_constant_identifier_names            , must_be_immutable, unnecessary_brace_in_string_interps

import 'package:easy_sidemenu/easy_sidemenu.dart';
import 'package:flutter/material.dart';
import 'package:project_award_statistics/utils/load_award_info_from_db/award_model.dart';
import 'package:project_award_statistics/utils/load_award_info_from_db/load_award_info.dart';
import 'package:window_manager/window_manager.dart';

class HomePage_Desktop extends StatefulWidget 
  {
    // 获取必需的学生名字
    final String stu_name;  

    HomePage_Desktop({
      super.key,
      required this.stu_name,
    });

    @override
    State<HomePage_Desktop> createState() => _HomePage_DesktopState();
  }

class _HomePage_DesktopState extends State<HomePage_Desktop> with TickerProviderStateMixin {

  // 初始化用户获奖信息列表对象
  List<award_model> user_award_list = List.empty(); 

  // 设置侧边栏所需的pageController与SideMenu类控制器
  PageController pageController=PageController();
  SideMenuController sideMenu=SideMenuController();

  // 初始化动画控制总线
  late AnimationController _animationController_widget1;  // 初始化动画总线1
  late AnimationController _animationController_widget2;  // 初始化动画总线2
  late AnimationController _animationController_widget3;  // 初始化动画总线3
  late AnimationController _animationController_widget4;  // 初始化动画总线4

  // 从Widget父类获取学生名字对象初始化
  late String stu_name;

  @override
  void initState ()  // 覆写类构造函数
    {
      // 覆写页面更新时行为
      sideMenu.addListener((index){
        pageController.jumpToPage(index);
      });

      // 初始化动画总线1
      _animationController_widget1 = AnimationController(
        duration: Duration(seconds: 2), // 设置动画持续时间
        vsync: this,
      );
      // 初始化动画总线2
      _animationController_widget2 = AnimationController(
        duration: Duration(seconds: 2), // 设置动画持续时间
        vsync: this,
      );
      // 初始化动画总线3
      _animationController_widget3 = AnimationController(
        duration: Duration(seconds: 2), // 设置动画持续时间
        vsync: this,
      );
      // 初始化动画总线4
      _animationController_widget4 = AnimationController(
        duration: Duration(seconds: 3), // 设置动画持续时间
        vsync: this,
      );

      // 动画,启动!
      _animationController_widget1.forward().then((_){
        Future.delayed(Duration(seconds: 0)).then((_){
          // 动画总线1启动后启动动画总线2
          _animationController_widget2.forward().then((_){
            Future.delayed(Duration(seconds: 0)).then((_){
              // 动画总线2启动后启动动画总线3
              _animationController_widget3.forward().then((_){
                Future.delayed(Duration(seconds: 0)).then((_){
                  // 动画总线3启动后启动动画总线4
                  _animationController_widget4.forward();
                });
              });
            });
          });
        });
      });

      // 更新user_award_list (用户获奖信息)列表,得到用户所有获奖情况
      update_user_award_list(); 

      // 从Widget父类获取学生名字并进行更新
      setState(() {
        stu_name=widget.stu_name;
      });
      super.initState();   
    }

  Future <void> update_user_award_list() async // 该函数用于获取用户的获奖信息
    {
      List<award_model> updated_user_award_list = await DBProvider_AwardInfo().get_award_list(widget.stu_name);
      setState(() {
        //更新statefulWidget里的user_award_list变量
        user_award_list = updated_user_award_list;
      });
    }

  void animation_dispose ()  // 如果动画未播放完就退出应用,那么先停止动画再释放动画总线对象
      {
        _animationController_widget1.stop();
        _animationController_widget2.stop();
        _animationController_widget3.stop();
        _animationController_widget4.stop();
        _animationController_widget1.dispose();
        _animationController_widget2.dispose();
        _animationController_widget3.dispose();
        _animationController_widget4.dispose();
      }

  @override
  void dispose () async // 覆写析构函数,在页面退出时关闭所有动画总线
    {
      animation_dispose();
      super.dispose();
    }   

  @override
  Widget build(BuildContext context) {
    // 更改窗口标题文字
    WindowManager.instance.setTitle("个人获奖信息查询  --   个人主页");
    // 初始化总项目
    return Scaffold( 
      backgroundColor: Colors.white,
      body: Padding(
        padding: const EdgeInsets.only(top:15.0,left:0.0),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [

            const SizedBox(width: 20,),   // 设置控件间距

            // 设置行式布局显示所有动画文本
            Padding(
              padding: const EdgeInsets.only(left:0.0,top:10.0),
              child: Column(   
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SlideTransition(
                    position: Tween<Offset>(
                      begin: Offset(-2.0, 0),
                      end: Offset.zero,
                    ).animate(
                      CurvedAnimation(
                        parent: _animationController_widget1,
                        curve: Curves.easeOut, // 设置特定动画曲线
                      ),
                    ),
                    child: Text(
                      "欢迎, ${stu_name}",
                      style: TextStyle(
                        fontSize: 60,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                  const SizedBox(height: 40,),
                  SlideTransition(
                    position: Tween<Offset>(
                      begin: Offset(-1.5, 0),
                      end: Offset.zero,
                    ).animate(
                      CurvedAnimation(
                        parent: _animationController_widget2,
                        curve: Curves.easeOut, // 设置特定动画曲线
                      ),
                    ),
                    child: Text(
                      "光阴似箭,日月如梭",
                      style: TextStyle(
                        fontSize: 40,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                  const SizedBox(height: 50,),   // 设置控件间一定距离
                  SlideTransition(
                    position: Tween<Offset>(
                      begin: Offset(-1.2, 0),
                      end: Offset.zero,
                    ).animate(
                      CurvedAnimation(
                        parent: _animationController_widget3,
                        curve: Curves.easeInOut, // 设置特定动画曲线
                      ),
                    ),
                    child: user_award_list.isEmpty   // 检查用户是否获得奖项,按获得与否分别输出不同文字
                    ?Text(
                      "在大学生活中,您还没有获得奖项",
                      style: TextStyle(
                        fontSize: 30,
                      ),
                    )
                    :Text(
                      "在大学生活中,您一共获得了个${user_award_list.length}个奖项",
                      style: TextStyle(
                        fontSize: 30,
                      ),
                    ),
                  ),
                  const SizedBox(height: 30,),
                  SlideTransition(
                    position: Tween<Offset>(
                      begin: Offset(-1.2, 0),
                      end: Offset.zero,
                    ).animate(
                      CurvedAnimation(
                        parent: _animationController_widget4,
                        curve: Curves.easeInOut, // 设置特定动画曲线
                      ),
                    ),
                    child: Text(
                      "希望你在接下来的学习生涯中坚持不懈,久久为功,赢得更多的奖项!",
                      style: TextStyle(
                        fontSize: 30,
                      ),
                    ),
                  ),
                ],
              ),
            ),   
          ],
        ),
      ),
    );
  }
}
